
<script setup>
	import { onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { formatDate, addLiveStreamVideoTimeCache } from "/request.js";
	import { updateADSDataByGroupID } from "/global.js";
	
	
	var groupId;
	onLoad(async function(options) {
		if (options.id)
			groupId = options.id;
	});
	
	
	var comment = "";
	var videoURL = "";
	var startTimeStr = "";
	var endTimeStr = "";
	
	async function clickAdd()
	{
		console.log("click add");
		console.log(comment);
		console.log(videoURL);
		console.log(startTime);
		console.log(endTime);
		
		
		
		if (!comment)
		{
			alert("任务备注为空");
			return;
		}
		if (!videoURL)
		{
			alert("视频链接为空");
			return;
		}
		if (!startTimeStr)
		{
			alert("开始时间为空");
			return;
		}
		if (!endTimeStr)
		{
			alert("结束时间为空");
			return;
		}
		
		
		
		var startTime = new Date(startTimeStr);
		var endTime = new Date(endTimeStr)
		/*
		var now = new Date();
		if (startTime < now)
		{
			alert("开始时间早于现在时间");
			return;
		}
		if (startTime > endTime)
		{
			alert("开始时间晚于结束时间");
			return;
		}
		if (startTime.getTime() == endTime.getTime())
		{
			alert("开始时间与结束时间相同");
			return;
		}
		*/
		
		var fStartTime = formatDate(startTime);
		var fEndTime = formatDate(endTime);
		console.log("开始时间： " + fStartTime);
		console.log("结束时间： " + fEndTime);
		
		var success = false;
		await addLiveStreamVideoTimeCache(
			groupId,
			comment,
			videoURL,
			fStartTime,
			fEndTime
		).then(function(type) {
			if (type === "Success")
				success = true;
		});
		if (success)
		{
			updateADSDataByGroupID(groupId);
			uni.navigateBack();
		}
	}
	
	
	function clickBack()
	{
		uni.navigateBack();
	}
</script>


<template>
	
	<header>
		<nav>
			<p>添加任务</p>
		</nav>
	</header>
	
	<main class="container">
		
		<view class="field">
			<label class="label">任务备注</label>
			<view class="control">
				<input class="input" type="text" placeholder="请输入" v-model="comment">
			</view>
		</view>
		
		<view class="field">
			<label class="label">视频链接</label>
			<view class="control">
				<input class="input" type="email" placeholder="Video URL" v-model="videoURL">
			</view>
		</view>
		
		<view class="field">
			<label class="label">开始时间</label>
			<view class="control">
				<input
					class="datetime-picker"
					type="datetime-local"
					name="starting-time"
					v-model="startTimeStr"
				/>
			</view>
		</view>
		
		
		<view class="field">
			<label class="label">结束时间</label>
			<view class="control">
				<input
					class="datetime-picker"
					type="datetime-local"
					name="end-time"
					v-model="endTimeStr"
				/>
			</view>
		</view>
		
		
		<view class="field is-grouped is-grouped-centered">
			<view class="control">
				<button class="button is-primary" v-on:click="clickAdd">添加</button>
			</view>
			<view class="control">
				<button class="button is-light" v-on:click="clickBack">返回</button>
			</view>
		</view>
		
	</main>
	
</template>



<style>
	
	nav {
		height: 6vh;
		background-color: whitesmoke;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		text-align: center;
	}
	
	
	.field {
		margin-top: 2vh;
	}
	
	.datetime-picker {
		height: 5vh;
	}
</style>

